package com.jn.picasso.presenter;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.JsonNull;
import com.jn.picasso.common.WeakRrfHandler;
import com.jn.picasso.contract.HeroContract;
import com.jn.picasso.entity.HeroEntity;
import com.jn.picasso.http.GlobalRetrofitClient;
import com.jn.picasso.service.HeroService;
import com.jn.picasso.utils.GsonUtils;
import com.jn.picasso.utils.LogUtils;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class HeroPresenter implements HeroContract.IHeroPresenter {
    public static final int HTTP_SUCCESS = 0;
    public static final int HTTP_SUCCESS2 = 10;
    public static final int HTTP_FAIL = 1;
    private HeroContract.IHeroView mHeroView;

    public HeroPresenter(HeroContract.IHeroView heroView) {
        mHeroView = heroView;
    }

    @Override
    public void getData(String heroName) {
        HeroService service = GlobalRetrofitClient.getInstance().create(HeroService.class);
        /*service.getHero(heroName).enqueue(new Callback<HeroEntity>() {
            @Override
            public void onResponse(Call<HeroEntity> call, Response<HeroEntity> response) {
                Message message = Message.obtain();
                message.obj = response.body();
                message.what = HTTP_SUCCESS;
                mHandler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<HeroEntity> call, Throwable t) {

            }
        });*/
        service.getHero2(heroName).enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String body = response.body().string();
                    LogUtils.d("asdf", "body = " + body);
                    Message message = Message.obtain();
                    message.obj = body;
                    message.what = HTTP_SUCCESS2;
                    mHandler.sendMessage(message);
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    WeakRrfHandler<HeroPresenter> mHandler = new WeakRrfHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int result = msg.what;
            switch (result) {
                case HTTP_SUCCESS:
                    HeroEntity hero = (HeroEntity) msg.obj;
                    if (mHeroView != null) {
                        mHeroView.showData(hero);
                    }
                    break;
                case HTTP_FAIL:
                    break;
                case HTTP_SUCCESS2:
                    String json = (String) msg.obj;
                    String jsonStr = json.replace("\\\"", "'");
                    HeroEntity heroEntity = JSON.parseObject(jsonStr, HeroEntity.class);
                    if (mHeroView != null) {
                        mHeroView.showData(heroEntity);
                    }
                    break;
            }
            return false;
        }
    });
}
